<?php

/**
* Controller:  "Pets"
*
* After a visitor selectes a pet from the front page, this controller is loaded,
* which extracts the selected pet's record from the database and displays all
* information and a picture of the pet.
*
* Functions:  index
*
* @author     Brandon A Johnson
* @copyright  LIS4368-01: "Web Development with PHP," Spring 2010
* @version    1.0
* @date       April 24, 2010
*
*/

class Pets extends Controller {

	/**
	* Pets() - constructor
	*
	* allows this file to act as a valid CodeIgniter controller
	*/
	function Pets()
	{
		parent::Controller();
	}
	
	/**
	* index()
	*
	* After a user selects a pet from the front page, that pet's record
	* is retrieved from the database and displayed here.
	*/
	function index()
	{
		// destroys the cookie session just in case someone clicks here from the secure section
		$this->load->library('session');
		$this->session->sess_destroy();

		// loads the "pets" database interaction model
		$this->load->model('pets_model');
		// extracts all pet records currently in the table
		$all_pets = $this->pets_model->list_pets();
		// prepares the data to be sent to the view
		$data = array();
		$data['results'] = $all_pets;
		
		// if no pet has been selected yet...
		if($_POST['pets'] == NULL)
		{
			// error message that displays when the page is reloaded
			$selected_pet[0]->Name = "No pet selected - please go back and try again.";
			
			// loads a special view that allows the user to try again
			$this->load->view('header');
			$this->load->view('no_pets', $data);
		}
		// otherwise...
		else
		{
			// prepares the user selection for the database query
			$pet_you_want = $_POST['pets'];
			// retrieve the appropriate record and send it for display
			$selected_pet = $this->pets_model->get_pet($pet_you_want);
			$data['choice'] = $selected_pet;
	
			// loads the view with the pet record
			$this->load->view('header');
			$this->load->view('pets', $data);
		}
	}
}

/* End of file pets.php */
/* Location: ./system/application/controllers/pets.php */